-- CALC_ARENA_AUDIENCE_BONUS

-- 计算角斗台观众掉落
-- @param user    玩家对象
-- @param args    奖励计算参数
return function(user, args)
    local randSeed  = DungeonM.getRandSeed("AUDIENCE_BONUS");
    local randSeed2 = DungeonM.getRandSeed("AUDIENCE_BONUS") % 1000;
    local amount;
    local classId;
    local bonus;
    local arr;

    local petId = args["pet_id"];

    -- 特殊观众
    if type(petId) == "number" then
        if args["kaitou"] == 1 then
            -- 如果是怪盗，根据选手冈布奥获得奖励
            local combatSlimeId = ArenaM.getCombatSlimeId();
            local bonusMap = {
                    [3013] = { 7289,7290,7291 },    --海格力斯冈布奥，金苹果
                    [3014] = { 5238, },             --阿波罗冈布奥，光系魔法书
                    [3015] = { 8141, },             --波塞冬冈布奥，海神之冠
                    [3016] = { 8140, },             --雅典娜冈布奥，神盾埃癸斯
                    [3018] = { 8268, },             --哈迪斯冈布奥，灵魂之瓶
                    [3019] = { 8111, },             --创世神冈布奥，知识の钥
                };
            local kaitouBonus = bonusMap[combatSlimeId];
            if kaitouBonus then
                -- 如果需要指定奖励
                classId = kaitouBonus[randSeed % #kaitouBonus + 1];
                return { 1, classId, 1, };
            else
                -- 否则给怪盗碎片
                amount  = randSeed % 2 + 1;
                classId = PetM.query(petId, "chips");
                return { 1, classId, amount, };
            end
        else
            -- 对应观众的碎片
            amount  = randSeed % 2 + 1;
            classId = PetM.query(petId, "chips");
            return { 1, classId, amount, };
        end
    end
    
    -- 获取玩家这一生已经通过角斗台获得的钻石数量
    local gemLifetime = ArenaM.getGainedGemLifetime();
    
    -- 获取本次迷宫已经通过角斗台获得的钻石数量
    local gemGained = ArenaM.getGainedGem();
    
    -- 获取本次角斗已出的钻石掉落
    local gemDroped = ArenaM.getDropedGem();
    
    -- 如果从来没有在角斗台获得过钻石，则本次必出1~2个
    if gemLifetime<= 0 then
        if gemDroped <= 0 then
            amount = randSeed % 2 + 1;
            bonus  = { 2, "gem", amount, };
            
            -- 统计已产出的钻石数量
            ArenaM.statDropedGem(amount);
            
            return bonus;
        end
    end

    if randSeed2 > 650 then
        -- 金币奖励
        amount = randSeed % 1001 + 500;
        bonus  = { 2, "money", amount, };
    elseif randSeed2 > 350 then
        -- 探索点奖励
        amount = randSeed % 101 + 50;
        bonus  = { 2, "search_point", amount, };
    elseif randSeed2 > 150 then
        -- 1阶食材奖励
        amount = randSeed % 11 + 5;
        arr = ItemM.getFoodMaterial(1);
        classId = FormulaM.invoke("FETCH_BY_RAND", arr, randSeed);
        bonus   = { 1, classId, amount, };
    elseif randSeed2 > 50 then
        -- 2阶食材奖励
        amount = randSeed % 5 + 3;
        arr = ItemM.getFoodMaterial(2);
        classId = FormulaM.invoke("FETCH_BY_RAND", arr, randSeed);
        bonus   = { 1, classId, amount, };
    else
        -- 玩家一生最多可获得的钻石
        local limitLifetime = 30;
        
        -- 每次迷宫最多能获得的钻石
        local limitGained = 6;
        
        if gemLifetime >= limitLifetime or gemGained >= limitGained then
            -- 本次迷宫已经获得的钻石数量已达上限
            -- 金币奖励
            amount = randSeed % 1001 + 500;
            bonus  = { 2, "money", amount, };
        else
            -- 钻石奖励
            amount = randSeed % 2 + 1;
            amount = math.min(amount, limitLifetime - gemLifetime, limitGained - gemGained);
            bonus  = { 2, "gem", amount, };
            
            -- 统计已产出的钻石数量
            ArenaM.statDropedGem(amount);
        end
    end

    return bonus;
end